﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace PaladinHelper
{
    public partial class Form1 : Form
    {

        //计时器
        System.Windows.Forms.Timer tim1 = new System.Windows.Forms.Timer();
        //计时器
        System.Windows.Forms.Timer tim2 = new System.Windows.Forms.Timer();

        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")] 
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        static extern IntPtr SetActiveWindow(IntPtr hWnd);
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool BringWindowToTop(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        [DllImport("user32.dll")]
        public static extern int PostMessage(IntPtr hWnd, int Msg, int wParam, uint lParam);
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool FlashWindow(IntPtr handle, bool bInvert);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        //键盘控制
        const int KEYEVENT_KEYDOWN = 0x0000; //按下
        const int KEYEVENT_KEYUP = 0x0002;  //抬起
        const int WM_KEYDOWN = 0x0100;
        const int WM_KEYUP = 0x0101;
        public const int WM_CHAR = 0x0102;      // 释放一个键

        int shiqu1 = 0;
        int shiqu2 = 0;

        bool zuobu1 = false;
        bool zuobu2 = false;
        int jishi = 0;

        int WowNub = 0;

        IntPtr[] WowHwnd = { IntPtr.Zero, IntPtr.Zero };

        /// <summary>
        /// 模拟一次键盘击键
        /// </summary>
        /// <param name="clickKey">发送字符的byte码</param>
        /// <returns></returns>
        public static void sendOneKey(byte clickKey, int sleep)
        {
            keybd_event(clickKey, (byte)0, KEYEVENT_KEYDOWN, 0);
            System.Threading.Thread.Sleep(sleep);
            keybd_event(clickKey, (byte)0, KEYEVENT_KEYUP, 0);
        }

        /// <summary>
        /// 鼠标键盘点击
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <param name="clickKey">键的ASIIC码</param>
        public static void PostOneKey(IntPtr hWnd, int clickKey, int sleep)
        {
            PostMessage(hWnd, WM_KEYDOWN, clickKey, 1);
            System.Threading.Thread.Sleep(sleep);
            PostMessage(hWnd, WM_KEYUP, clickKey, 1);
        }


        public Form1()
        {
            InitializeComponent();



            IntPtr[] Hwnd = { IntPtr.Zero, IntPtr.Zero };

            //   IntPtr Hwnd= IntPtr.Zero;//窗口句柄
            //     Hwnd = FindWindow(null, "魔兽世界");
            string processName = "WowClassic";
            Process[] arrayProcess = Process.GetProcessesByName(processName);
            foreach (Process p in arrayProcess)
            {
                if (p.MainWindowHandle != IntPtr.Zero)
                {
                    Hwnd[WowNub] = p.MainWindowHandle;
                    WowHwnd[WowNub] = Hwnd[WowNub];
                    //FlashWindow(Hwnd[i], true);

                    ShowWindow(WowHwnd[WowNub], 9);//5

                    SetActiveWindow(WowHwnd[WowNub]);

                    BringWindowToTop(WowHwnd[WowNub]);

                    SetForegroundWindow(WowHwnd[WowNub]);

              //      SwitchToThisWindow(Hwnd[WowNub], true);
                    System.Threading.Thread.Sleep(800);           
                    MessageBox.Show("发现第" + (WowNub + 1).ToString() + "个WOW游戏窗口", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                    System.Threading.Thread.Sleep(1000);
                    WowNub++;
                }

            }

            if (WowNub == 0)
            {
                MessageBox.Show("没找到游戏，先开游戏再开助手！");
                System.Environment.Exit(0);
            }


            //注册热键Shift+S，Id号为100。HotKey.KeyModifiers.Shift也可以直接使用数字4来表示。
            HotKey.RegisterHotKey(Handle, 1112, 0, Keys.F1);
            //注册热键Ctrl+B，Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。
            HotKey.RegisterHotKey(Handle, 1113, 0, Keys.F2);
            //注册热键Ctrl+B，Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。
            HotKey.RegisterHotKey(Handle, 1114, 0, Keys.F3);
            //注册热键Ctrl+B，Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。
            HotKey.RegisterHotKey(Handle, 1115, 0, Keys.F4);



            tim1.Interval = 50;
            tim1.Tick += delegate                 //定时器
            {
                if (shiqu1 == 1)
                {


                    PostOneKey(Hwnd[0], 97, 50);   //数字键盘1

                }
                if (shiqu2 == 1)
                {

                    PostOneKey(Hwnd[1], 97, 50);//数字键盘1

                }
            };


                tim2.Interval = 1000;
                tim2.Tick += delegate                 //定时器
                {

                    if (zuobu1 == true)
                    {
                        jishi++;
                        if (jishi++ > 30)  //30秒后做布
                        {
                            PostOneKey(Hwnd[0], 96, 50); //小键盘0  //做布
                            System.Threading.Thread.Sleep(200);
                            PostOneKey(Hwnd[0], 96, 50); //小键盘0  //做布
                            zuobu1 = false;
                            jishi = 0;
                            tim2.Stop();
                        }
                    }

                    if (zuobu2 == true)
                    {
                        jishi++;
                        if (jishi++ > 30) //30秒后做布
                        {
                            PostOneKey(Hwnd[1], 96, 50); //小键盘0  //做布
                            System.Threading.Thread.Sleep(200);
                            PostOneKey(Hwnd[1], 96, 50); //小键盘0  //做布
                            zuobu2 = false;
                            jishi = 0;
                            tim2.Stop();
                        }
                    }


                };


        }

        // 修理
        void xiuli(int i)
        {
            tim1.Stop();

            PostOneKey(WowHwnd[i], 98, 50); //小键盘2  //重置  目标机器人  开启点击移动
            System.Threading.Thread.Sleep(200);
            PostOneKey(WowHwnd[i], 99, 50); //小键盘3  //与目标互动
            System.Threading.Thread.Sleep(200);
            PostOneKey(WowHwnd[i], 98, 50); //小键盘2  //重置  目标机器人  关闭点击移动
            if (i == 0)  //做布1
            {
                jishi = 0;
                zuobu1 = true;
                zuobu2 = false;
            }
            else       //做布2
            {
                jishi = 0;
                zuobu2 = true;
                zuobu1 = false;
            }

            tim2.Start();
        }

        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 1112:    //按下的是F1
                            if (shiqu1 == 0)
                            {
                                tim1.Start();
                                shiqu2 = 0;
                                shiqu1 = 1;
                            }
                            else
                            {
                                shiqu1 = 0;
                                shiqu2 = 0;
                                tim1.Stop();
                            }
                            break;
                        case 1113:    //按下的是
                            if (shiqu2 == 0)
                            {
                                tim1.Start();
                                shiqu1 = 0;
                                shiqu2 = 1;
                            }
                            else
                            {
                                shiqu2 = 0;
                                shiqu1 = 0;
                                tim1.Stop();
                            }
                            break;
                        case 1114:    //按下的是Alt+D
                            xiuli(0);
                            break;
                        case 1115:    //按下的是Alt+D
                            xiuli(1);
                            break;
                    }
                    break;
            }
            base.WndProc(ref m);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //注销Id号为100的热键设定
            HotKey.UnregisterHotKey(Handle, 1112);
            //注销Id号为101的热键设定
            HotKey.UnregisterHotKey(Handle, 1113);
            //注销Id号为101的热键设定
            HotKey.UnregisterHotKey(Handle, 1114);
            //注销Id号为101的热键设定
            HotKey.UnregisterHotKey(Handle, 1115);

        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            // 判断只有最小化时，隐藏窗体
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void 双骑助手_Click(object sender, EventArgs e)
        {
            // 正常显示窗体
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }
    }
}
